<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Gotcha\App\Framework;

// Gotcha framework

use Gotcha\App\Extensions\GotchaCulturesExtension;
use Gotcha\App\Extensions\GotchaErrorsExtension;
use Gotcha\App\Extensions\GotchaFileSystemExtension;
use Gotcha\App\Extensions\GotchaLogsExtension;
use Gotcha\App\Extensions\GotchaThemesExtension;
use Gotcha\App\GotchaBootstrap;
use Gotcha\App\Types\String;

class Controller extends GotchaBootstrap {
    // View file name handler
    protected $view_file_name_handler = '';

    // Accepted methods
    private $accepted_methods = array('View', 'UrlContent', 'nameToSlug', 'slugToName');

    // Methods overloader
    public function __call($method, $params) {
        if (in_array($method, $this->accepted_methods)) {
            switch ($method) {
                case 'View' :
                    switch(count($params)) {
                        case 0:
                            $this->ViewDefault();
                            break;
                        default:
                            GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_AMOUNT'))), new String('GotchaFramework::'.$method.'()'), new String(count($params)));
                            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_AMOUNT'))), new String('GotchaFramework::'.$method.'()'), new String(count($params)));
                            exit;
                    }
                    break;
                case 'UrlContent':
                    switch(count($params)) {
                        case 1:
                            switch(gettype($params[0])) {
                                case 'string':
                                    return $this->UrlContentString($params[0]);
                                    break;
                                default:
                                    GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_TYPE')), new String('GotchaFramework::'.$method.'()'), new String(count($params)), new String(gettype($params[0]))));
                                    GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_TYPE')), new String('GotchaFramework::'.$method.'()'), new String(count($params)), new String(gettype($params[0]))));
                                    exit;
                            }
                            break;
                        default:
                            GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_AMOUNT')), new String('GotchaFramework::'.$method.'()'), new String(count($params))));
                            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_AMOUNT')), new String('GotchaFramework::'.$method.'()'), new String (count($params))));
                            exit;
                    }
                break;
                case 'nameToSlug':
                    switch(count($params)) {
                        case 1:
                            switch(gettype($params[0])) {
                                case 'string':
                                    return $this->nameToSlugDefault($params[0]);
                                    break;
                                default:
                                    GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_TYPE')), new String('GotchaFramework::'.$method.'()'), new String(count($params)), new String(gettype($params[0]))));
                                    GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_TYPE')), new String('GotchaFramework::'.$method.'()'), new String(count($params)), new String(gettype($params[0]))));
                                    exit;
                            }
                            break;
                        default:
                            GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_AMOUNT')), new String('GotchaFramework::'.$method.'()'), new String(count($params))));
                            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_AMOUNT')), new String('GotchaFramework::'.$method.'()'), new String (count($params))));
                            exit;
                    }
                break;
                case 'slugToName':
                    switch(count($params)) {
                        case 1:
                            switch(gettype($params[0])) {
                                case 'string':
                                    return $this->slugToNameDefault($params[0]);
                                    break;
                                default:
                                    GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_TYPE')), new String('GotchaFramework::'.$method.'()'), new String(count($params)), new String(gettype($params[0]))));
                                    GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_TYPE')), new String('GotchaFramework::'.$method.'()'), new String(count($params)), new String(gettype($params[0]))));
                                    exit;
                            }
                            break;
                        default:
                            GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_AMOUNT')), new String('GotchaFramework::'.$method.'()'), new String(count($params))));
                            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_INVALID_PARAMETER_AMOUNT')), new String('GotchaFramework::'.$method.'()'), new String (count($params))));
                            exit;
                    }
                    break;
            }
        } else {
            GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_NO_METHOD'))), new String('GotchaFramework::'.$method));
            GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('OVERLOAD_ERROR_NO_METHOD'))), new String('GotchaFramework::'.$method));
            exit;
        }
    }

    private function ViewDefault() {
    // If there is no view given, method
    // searches for a view stored in
    // Modules/[MODULE_NAME]/View/[CONTROLLER_NAME]/

        // We use debug_backtrace() to get method
        // and controller names
        $backtrace = debug_backtrace();

        // Since debug_backtrace() returns an array,
        // we are interested in second key
        $backtrace_function = $backtrace[3]['function'];
        $backtrace_class = $backtrace[3]['class'];

        // Class is returned with whole namespace
        // so there is a need to clean it up
        $backtrace_class_exploded = explode('\\', $backtrace_class);
        $backtrace_class = str_replace('Controller', '', $backtrace_class_exploded[count($backtrace_class_exploded) - 1]);

        // Now when we have both class - which represents
        // subdirectory in View directory - and function name
        // - which represent view name - we can load a view
        if (GotchaFileSystemExtension::CheckDirExistence(new String('Modules/'.$backtrace_class_exploded[1].'/Views/'.$backtrace_class)) === true) {
            // Corresponding directory exists
            if (GotchaFileSystemExtension::CheckFileExistence(new String($backtrace_function.'.phtml'), new String('Modules/'.$backtrace_class_exploded[1].'/Views/'.$backtrace_class.'/'))) {
                // View file exists
                $this->view_file_name_handler = 'Modules/'.$backtrace_class_exploded[1].'/Views/'.$backtrace_class.'/'.$backtrace_function.'.phtml';
            } else {
                GotchaLogsExtension::createNewLogEntry(new String(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Modules/'.$backtrace_class_exploded[1].'/Views/'.$backtrace_class.'/'.$backtrace_function.'.phtml')));
                GotchaErrorsExtension::throwError(new String(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_FILE_DOESNT_EXIST')), new String('Modules/'.$backtrace_class_exploded[1].'/Views/'.$backtrace_class.'/'.$backtrace_function.'.phtml')));
                exit;
            }
        } else {
            GotchaLogsExtension::createNewLogEntry(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_DIR_DOESNT_EXIST')), new String('Modules/'.$backtrace_class_exploded[1].'/Views/'.$backtrace_class));
            GotchaErrorsExtension::throwError(GotchaCulturesExtension::getTranslation(new String('REQUESTED_VIEW_DIR_DOESNT_EXIST')), new String('Modules/'.$backtrace_class_exploded[1].'/Views/'.$backtrace_class));
            exit;
        }

        // Now when we have handler for a view file
        // we can go further and enable whole themed
        // stuff
        $view_layout = new GotchaThemesExtension();
        $view_layout->GenerateViewLayout(new String($this->view_file_name_handler));
    }

    private function UrlContentString($url) {
        return GOTCHA_BASE_PATH.$url;
    }

    private function nameToSlugDefault($name) {
        // Function converts given name to slug
        // which can be used with URLs
        $name = strtolower($name);

        // Remove all non-alphabetical characters
        preg_replace("/[^A-Za-z0-9 ]/", '', $name);

        // Explode and then implode string
        $name_words = explode(' ', $name);
        $name_slug = implode('_', $name_words);

        return $name_slug;
    }

    private function slugToNameDefault($slug) {
        // Function converts given slug to
        // regular string
        $slug_words = explode('_', $slug);
        $slug_name = implode(' ', $slug_words);

        return $slug_name;
    }
}